package version1_3_1;

import version1_1.*;
import version1_2_1.People;

import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Callable;

/**
 * @program:IntelliJ IDEA
 * @Description:使用第三种创建线程的方式
 * @author:Mr.CheCheHuiBujianO
 * @data: 2022/7/13 11:21
 */
public class GameThread3 implements Callable<String> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public String call() throws Exception {
        int gameTime = 0;//游戏进行局数
        int gameWinTime = 0;//游戏胜利局数
        int gameLoseTime = 0;//游戏失败局数

        //使用多态的写法进行对象的创建
        User person = new People();
        person.setName();//设置名字
        person.getId();//设置id

        User computer = new Computer();
        computer.setName();
        computer.getId();

        GestureTransition gestureTransition = new GestureTransitionIMPH();
        GameJudge gameJudge = new GameJudgeIMPH();

        //实现游戏结果的存储功能
        GameFileOperate1 gameFileOperate1 = new GameFileOperate1();
        gameFileOperate1.createFileBuffer();//首先创建文件
        //游戏环节
        while(gameTime < 5 && gameWinTime < 3 && gameLoseTime < 3) {
            person.setGesture();//人的手势
            computer.setGesture();//电脑手势
            //将手势转化为int类型
            int personGesture = gestureTransition.gesture(person.getGesture());
            int computerGesture = gestureTransition.gesture(computer.getGesture());
            //将转化后的手势扔进判断函数进行判断
            String gameResult = gameJudge.gameJudge(personGesture, computerGesture);
            gameFileOperate1.FileBufferWriter(gameResult);//将数据写入我们的文件中
            switch (gameResult) {
                case "胜利":
                    gameWinTime++;
                    gameTime++;
                    new GameJudgeIMPH().printResult(gameWinTime, gameLoseTime, gameTime, gameResult);
                    break;
                case "失败":
                    gameLoseTime++;
                    gameTime++;
                    new GameJudgeIMPH().printResult(gameWinTime, gameLoseTime, gameTime, gameResult);
                    break;
                case "平局":
                    gameTime++;
                    new GameJudgeIMPH().printResult(gameWinTime, gameLoseTime, gameTime, gameResult);
                    break;
                default:
                    System.out.println("请检查你的输入！！！");
            }
        }
        String gameResult = null;
        if (gameWinTime == 3){
            System.out.println("恭喜你这句游戏，"+person.getName()+"胜利");
            gameResult = "胜利";
        }else if (gameLoseTime == 3){
            System.out.println("对不起这句游戏"+person.getName()+"失败"+computer.getName()+"胜利");
            gameResult = "失败";
        }else if (gameTime == 5 && gameWinTime>gameLoseTime){
            System.out.println("恭喜你这句游戏，"+person.getName()+"胜利");
            gameResult = "胜利";
        }else if (gameTime == 5 && gameLoseTime > gameWinTime){
            System.out.println("对不起这句游戏"+person.getName()+"失败"+computer.getName()+"胜利");
            gameResult = "失败";
        }else if (gameTime == 5 && gameLoseTime == gameWinTime){
            System.out.println("不好意思，这句游戏,"+person.getName()+"和"+computer.getName()+"打成平局");
            gameResult = "平局";
        }
        int choose = -1;//一个选择查看的参数
        while(choose != 0){
            System.out.println("请输入你想找第几局的游戏结果:输入0是退出查看");
            Scanner in = new Scanner(System.in);
            try {
                choose = in.nextInt();
            } catch (Exception e) {
                System.out.println("请检查你的输入");
                in.nextLine();
            }
            Map<Integer, String> integerStringMap = gameFileOperate1.FileBufferReader();
            System.out.println(integerStringMap.get(choose));
        }
        return gameResult;
    }
}